<?php
namespace Macaca\Module\Admin;

use Library\Filter;
use Macaca;
use Monkey;

class Goods {

    /**
     * @var Monkey\App
     */
    public $app;

    private $modelGoods; //'g_id','gc_id','g_name','captain','detail','image','price','keywords','description','add_time','top_sort','click'
    private $modelPriKey;
    private $modelCacheKey = 'MacacaGoods';

    /**
     * @param Monkey\App $app
     */
    public function __construct($app) {
        $this->app = $app;
        $this->modelGoods = new Macaca\Model\Admin\Goods($app);
        $this->modelPriKey = $this->modelGoods->priKey;
    }

    /**
     * @param int $page
     * @param int $pageLength
     *
     * @return array
     */
    public function getGoodsList($page = 0, $pageLength = 10) {
        //        $result = array();
        //        $cache = $this->app->cache();
        //        if (!$cache->fetch($this->modelCacheKey, $result)) {
        //            $result = $this->modelGoods->findList();
        //            $cache->store($this->modelCacheKey, $result);
        //        }
        return $this->modelGoods->findList($page, $pageLength);
    }

    /**
     * @return int
     */
    public function getCount() {
        $result = 0;
        $cache = $this->app->cache();
        if (!$cache->fetch($this->modelCacheKey . 'count', $result)) {
            $result = $this->modelGoods->count();
            $cache->store($this->modelCacheKey . 'count', $result);
        }
        return $result;
    }

    /**
     * 获取一个空页面 //'g_id','gc_id','g_name','captain','detail','image','price','keywords','description','add_time','top_sort','click'
     * @return mixed
     */
    public function getEmptyItem() {
        $data['g_id'] = '';
        $data['gc_id'] = '';
        $data['g_name'] = '';
        $data['captain'] = '新商品';
        $data['detail'] = '';
        $data['image'] = '';
        $data['price'] = '0.00';
        $data['keywords'] = 'Macaca 建站系统';
        $data['description'] = 'Macaca 建站系统';
        $data['add_time'] = $this->app->TIME;
        $data['top_sort'] = '0';
        $data['click'] = '0';
        return $data;
    }

    public function getItemById($gId) {
        $data = $this->modelGoods->find($this->modelPriKey, $gId);

        if ($data) {
            return $data;
        }
        else {
            return array();
        }
    }

    public function deleteItemById($gId) {
        if ($this->modelGoods->delete($this->modelPriKey, $gId)) {
            $this->resetCache();
            return true;
        }
        else {
            return false;
        }
    }

    public function saveItem($data = array()) {
        $pk = $this->modelPriKey;

        if (!empty($data[$pk]) and !is_numeric($data[$pk])) {
            return false;
        }

        $dataTemp = $this->getEmptyItem();
        unset($dataTemp[$pk]);
        foreach ($dataTemp as $k => $v) {
            if (!isset($data[$k])) {
                return false;
            }
            $dataTemp[$k] = $data[$k];
        }

        $dataTemp['detail'] = Filter::compressWhite($dataTemp['detail']);
        $dataTemp['detail'] = Filter::xssToHtml($dataTemp['detail']);

        if (!empty($data[$pk])) {
            //删除过期图片
            $oldData = $this->getItemById($data[$pk]);
            if (isset($oldData['image']) and $oldData['image'] != $dataTemp['image']) {
                unlink(url_to_file($oldData['image']));
            }
            $result = $this->modelGoods->updateByPriKey($data[$pk], $dataTemp);
        }
        else {
            $result = $this->modelGoods->insert($dataTemp);
        }

        if ($result) {
            $this->resetCache();
        }

        return $result;
    }


    protected function resetCache() {
        $this->app->cache()->delete($this->modelCacheKey . 'count');
    }
} 